//
//  USTarotCardPreview.m
//  us
//
//  Created by karry on 2025/5/14.
//

#import "USTarotCardPreview.h"
#import "USTarotCardManager.h"
#import "USTarotCard.h"
#import "AIManager.h"

@interface USTarotCardPreview()

@property (nonatomic, assign) SystemSoundID soundID;

@property (nonatomic, strong) UILabel *titleLab;

@property (nonatomic, strong) UILabel *subLab;


@end

@implementation USTarotCardPreview

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (void)playClickSound {
    if (self.soundID) {
        AudioServicesPlaySystemSound(self.soundID);
    }
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        // 1️⃣ 加载音效文件
        NSString *soundPath = [[NSBundle mainBundle] pathForResource:@"fanka" ofType:@"mp3"];
        if (soundPath) {
            NSURL *soundURL = [NSURL fileURLWithPath:soundPath];
            AudioServicesCreateSystemSoundID((__bridge CFURLRef)soundURL, &_soundID);
        }
        [self setupView];
    }
    return self;
}


- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"taluo_cover_0"]];
        _imageView.layer.cornerRadius = 6;
        _imageView.layer.masksToBounds = YES;
        

        
    }
    return _imageView;
}

- (void)setupView{
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.contentView = [[UIVisualEffectView alloc] initWithEffect:blur];
    [self addSubview:self.contentView];

    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.closeBtn addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside];
    [self.closeBtn setImage:[UIImage imageNamed:@"ic_close"] forState:0];
    [self.contentView.contentView addSubview:self.closeBtn];
    
    [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(self.contentView.mas_safeAreaLayoutGuideTop).mas_offset(20);
    }];
    
    [self addSubview:self.imageView];
    
    UIButton *actionBtn = [UIButton us_fillButtonWithTitle:@"确定"];
    [actionBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView.contentView addSubview:actionBtn];
    
    [actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.height.mas_equalTo([UIButton us_buttonDefaultHeight]);
        make.bottom.mas_equalTo(self.mas_safeAreaLayoutGuideBottom).mas_offset(-12);
    }];
 
    self.titleLab = [UILabel new];
    self.titleLab.font = [UIFont systemFontOfSize:22 weight:UIFontWeightSemibold];
    self.titleLab.textAlignment = NSTextAlignmentCenter;
    [self.contentView.contentView addSubview:self.titleLab];
    
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(20);
        make.right.mas_offset(-20);
        make.top.mas_equalTo(self.contentView.contentView.mas_safeAreaLayoutGuideTop).mas_offset(20);
    }];
    
    self.subLab = [UILabel new];
    self.subLab.textColor = [UIColor secondaryLabelColor];
    self.subLab.font = [UIFont systemFontOfSize:17];
    [self.contentView.contentView addSubview:self.subLab];
    [self.subLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLab.mas_bottom).mas_offset(6);
        make.centerX.mas_equalTo(self.titleLab);
    }];
    
}

- (void)sureAction:(UIButton *)sender{
    
    [USFeedbackTool notifySuccess];
    sender.userInteractionEnabled = NO;

    [self playClickSound];
    
    USTarotCard *card = [USTarotCardManager sharedManager].drawRandomCard;
    
    UIImage *nextImage = [UIImage imageNamed:card.imageName]; // 替换为你想显示的下一张图片
    if (card.orientation == USTarotCardOrientationReversed) {
        nextImage = [UIImage imageWithCGImage:nextImage.CGImage scale:nextImage.scale orientation:UIImageOrientationDown];
    }
    self.titleLab.text = card.name;
    self.subLab.text = card.orientation == USTarotCardOrientationReversed?@"逆位":@"正位";
    // 2. 翻转动画
    [UIView transitionWithView:self.imageView
                      duration:0.6
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
        self.imageView.image = nextImage;
    } completion:^(BOOL finished) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self goToNext:card];
        });

    }];
    
}

- (void)goToNext:(USTarotCard *)card{
    
    !self.didSeletedCardBlock?:self.didSeletedCardBlock(card);
    [self removeFromSuperview];
}


- (void)closeAction{
    !self.closeBlock?:self.closeBlock();
}

@end
